package utils;




 import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import aad.storyboard.model.Chapter;
import aad.storyboard.model.Scene;
import aad.storyboard.model.Script;
import aad.storyboard.model.Story;

 public class MyXMLHandler extends DefaultHandler {

 Boolean currentElement = false;
 String currentValue = null;
 
 String text;
 int sceneStartTime;
 int scriptStartTime;
 int scriptEndTime;
 Chapter chapter=null;
 Scene scene=null;
 Script script=null;
 int chapterCounter = 1;
 int sceneCounter = 1;

 
 public void getXMLdata(InputSource input)
 {
         try {

                  /** Handling XML */
                 
                  InputSource is = input;;        
                  SAXParserFactory spf = SAXParserFactory.newInstance();
                  SAXParser sp = spf.newSAXParser();
                  XMLReader xr = sp.getXMLReader();
                  
                  MyXMLHandler myXMLHandler = new MyXMLHandler();
                  xr.setContentHandler(myXMLHandler);
                  xr.parse(new InputSource(is.getByteStream()));

                  } catch (Exception e) {
                  System.out.println("XML Pasing Excpetion = " + e);
                  }
               
 }
 
 @Override
 public void startElement(String uri, String name, String qName,
 Attributes attributes) throws SAXException {
         
         currentElement = true;  
         if (name.equals("Chapter"))     {   
                 chapter=new Chapter(chapterCounter++);           
        
     }
         else if (name.equals("Scene"))
     {
        scene=new Scene(sceneCounter++);
        sceneStartTime=Integer.parseInt(attributes.getValue("startTime"));
        System.out.println("setting id "+Integer.parseInt(attributes.getValue("number")));
        scene.setId(Integer.parseInt(attributes.getValue("number")));
        scene.setStartTime(sceneStartTime); 
        
     }
         else if (name.equals("Script"))
     {
        script =new Script();
        scriptStartTime=Integer.parseInt(attributes.getValue("startTime"));
        scriptEndTime=Integer.parseInt(attributes.getValue("endTime")); 
        script.setStartTime(scriptStartTime);
        script.setEndTime(scriptEndTime);
        
     }
         else if (name.equals("Text"))
     {
        text=attributes.getValue("text");
        
     }     
     
         }
 

         /** Called when tag closing ( ex:- <name>AndroidPeople</name>
         * -- </name> )*/
         @Override
         public void endElement(String uri, String localName, String qName)
         throws SAXException {

         currentElement = false;
         
         if (localName.equalsIgnoreCase("Chapter"))
         {
                 Story.getInstance().addChapter(chapter);
                
         }

         if (localName.equalsIgnoreCase("Scene"))
         {
                 chapter.addScene(scene);
                
         }
        
         else if (localName.equalsIgnoreCase("Script"))
         {
                
                scene.addScript(script);
                
         }
         else if (localName.equals("Text"))
     {
        script.setText(text);
        
     }   
         }

         /** Called to get tag characters ( ex:- <name>AndroidPeople</name>
         * -- to get AndroidPeople Character ) */
         @Override
         public void characters(char[] ch, int start, int length)
         throws SAXException {

         if (currentElement) {
         currentValue = new String(ch, start, length);
         currentElement = false;

 }

 }

 }
